package com.example.service;

import com.example.entity.Review;
import com.example.mapper.ReviewMapper;
import com.example.mapper.UserMapper;
import com.example.mapper.HotelMapper;
import com.example.mapper.RoomMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 评价服务类
 */
@Service
public class ReviewService {

    @Resource
    private ReviewMapper reviewMapper;
    
    @Resource
    private UserMapper userMapper;
    
    @Resource
    private HotelMapper hotelMapper;
    
    @Resource
    private RoomMapper roomMapper;

    /**
     * 新增评价
     */
    public void add(Review review) {
        // 设置评价时间
        review.setReviewTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        reviewMapper.insert(review);
    }

    /**
     * 更新评价回复
     */
    public void update(Review review) {
        if (review.getReply() != null) {
            // 设置回复时间
            review.setReplyTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        }
        reviewMapper.updateById(review);
    }

    /**
     * 删除评价
     */
    public void deleteById(Long id) {
        reviewMapper.deleteById(id);
    }

    /**
     * 根据ID查询评价
     */
    public Review selectById(Long id) {
        return reviewMapper.selectById(id);
    }

    /**
     * 查询所有评价
     */
    public List<Review> selectAll(Review review) {
        List<Review> reviews = reviewMapper.selectAll(review);
        // 填充扩展信息
        for (Review r : reviews) {
            enrichReviewInfo(r);
        }
        return reviews;
    }

    /**
     * 根据用户ID查询评价列表
     */
    public List<Review> selectByUserId(Long userId) {
        List<Review> reviews = reviewMapper.selectByUserId(userId);
        // 填充扩展信息
        for (Review review : reviews) {
            enrichReviewInfo(review);
        }
        return reviews;
    }

    /**
     * 根据酒店ID查询评价列表
     */
    public List<Review> selectByHotelId(Long hotelId) {
        List<Review> reviews = reviewMapper.selectByHotelId(hotelId);
        // 填充扩展信息
        for (Review review : reviews) {
            enrichReviewInfo(review);
        }
        return reviews;
    }

    /**
     * 根据预订ID查询评价
     */
    public Review selectByBookingId(Long bookingId) {
        return reviewMapper.selectByBookingId(bookingId);
    }

    /**
     * 分页查询评价列表
     */
    public PageInfo<Review> selectPage(Review review, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Review> list = reviewMapper.selectAll(review);
        return PageInfo.of(list);
    }

    // 填充评价的扩展信息
    private void enrichReviewInfo(Review review) {
        if (review.getUserId() != null) {
            review.setUserName(userMapper.selectById(review.getUserId().intValue()).getName());
        }
        if (review.getHotelId() != null) {
            review.setHotelName(hotelMapper.selectById(review.getHotelId()).getName());
        }
        if (review.getRoomId() != null) {
            review.setRoomType(roomMapper.selectById(review.getRoomId()).getRoomType());
        }
    }
} 